﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class FloorBLL
    {
        private static int floorID = 0;
        GaraDataContext DB = new GaraDataContext();

        public IQueryable SelectAll()
        {
            return (from p in DB.Floors select p).OrderByDescending(s=>s.FloorID).AsQueryable();
        }

        public string Insert(FloorDTO floorDTO)
        {
            try
            {
                Floor floor = new Floor();
                floor.FloorID = floorID++;
                floor.NumberFloor = floorDTO.NumberFloor;
                floor.Status = floorDTO.Status;

                DB.Floors.InsertOnSubmit(floor);
                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        public string Update(FloorDTO floorDTO)
        {
            try
            {
                Floor floor = DB.Floors.Where(s => s.FloorID == floorDTO.FloorID).FirstOrDefault();
                floor.NumberFloor = floorDTO.NumberFloor;
                floor.Status = floorDTO.Status;

                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        public string Delete(FloorDTO floorDTO)
        {
            try
            {
                Floor floor = DB.Floors.Where(s => s.FloorID == floorDTO.FloorID).FirstOrDefault();

                DB.Floors.DeleteOnSubmit(floor);
                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        public Boolean CheckFloorExist(int floor)
        {
            int count = DB.Floors.Where(s => s.NumberFloor == floor).Count();
            if (count > 0) return true;
            else return false;
        }

        public List<FloorDTO> FillDataCombobox()
        {
            return (from p in DB.Floors
                    where p.Status == "Sử dụng"
                    select new FloorDTO
                    {
                        FloorID = p.FloorID,
                        NumberFloor = Convert.ToInt32(p.NumberFloor)
                    }).Distinct().ToList<FloorDTO>();
        }
    }
}
